package org.eclipse.epsilon.emc.lom;


import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.eclipse.emf.common.notify.Adapter;
import org.eclipse.emf.common.notify.AdapterFactory;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.Notifier;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.TreeIterator;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.EPackage.Registry;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.URIConverter;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.xmi.XMLOptions;
import org.eclipse.emf.ecore.xmi.XMLResource;
import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;


public class EmfWorkbench {
	
	public static void main(String[] args) throws Exception {
		EmfWorkbench workbench = new EmfWorkbench();
		Resource resource = workbench.loadModel();
		//EObject object = null;
		//printAttributeValues(resource);
		LiveObjectModelBuilder x = new LiveObjectModelBuilder("E:/MSc_IT/Spring term/swi/projects/org.eclipse.epsilon.emc.lom/db/test");
		x.createDatabase();
		x.addResource(resource);
	}
	
	protected String base = "E:/MSc_IT/Spring term/swi/projects/org.eclipse.epsilon.emc.lom/model/";
	//protected String base = "D:/Projects/Eclipse/3.5.1/workspace/org.eclipse.epsilon.emc.lom/model/";
	
	/**
	 * Get an EPackage from the metamodel file
	 * @return
	 * @throws Exception
	 */
	public EPackage registerMetamodel() throws Exception {
		
		String ecore = base + "library.ecore";
		
		ResourceSet resourceSet = new ResourceSetImpl();
		resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("*", new XMIResourceFactoryImpl());
		
		Resource resource = resourceSet.createResource(URI.createFileURI(ecore));
		resource.load(null);
		
		return (EPackage) resource.getContents().get(0);
		
	}
	
	/**
	 * Load MusicLibrary.xmi using the metamodel loaded in registerMetamodel
	 * @return
	 * @throws Exception
	 */
	public Resource loadModel() throws Exception {
		
		String model = base + "myLibrary.model";
		
		ResourceSet resourceSet = new ResourceSetImpl();
		resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().
			put("*", new XMIResourceFactoryImpl());
		
		EPackage metamodel = registerMetamodel();
		resourceSet.getPackageRegistry().put(metamodel.getNsURI(), metamodel);
		
		Resource resource = resourceSet.createResource(URI.createFileURI(model));
		
		resource.load(null);
		
		return resource;
		
	}
	
	
}
		
		


